package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.common.vo.PicUploadResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
public class UserInfoService {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FaceEngineService faceEngineService;
    @Autowired
    private PicUploadService picUploadService;

    public ErrorResult saveLoginReginfo(Map<String, String> param) {
        User user = UserThreadLocal.get();
        //为null就Error
        if (user == null) return ErrorResult.builder().errCode("0007").errMessage("用户校验失败,请重新登入!").build();
        UserInfo userInfo = null;

        //添加数据库
        try {
            userInfo = new UserInfo();
            userInfo.setSex("man".equals(param.get("gender")) ? SexEnum.MAN : SexEnum.WOMAN);
            userInfo.setNickName(param.get("nickname"));
            userInfo.setBirthday(param.get("birthday"));
            userInfo.setCity(param.get("city"));
            userInfo.setUserId(user.getId());
        } catch (Exception e) {
            log.error("用户信息添加失败!");
            e.printStackTrace();
        }
        int insert = userInfoMapper.insert(userInfo);
        //添加失败,返回Error
        if (insert != 1) {
            return ErrorResult.builder().errCode("008").errMessage("用户注册失败!").build();
        }
        return null;
    }

    //保存图片
    public ErrorResult saveLogo(MultipartFile file) {
        User user = UserThreadLocal.get();
        //为null就Error
        if (user == null) return ErrorResult.builder().errCode("007").errMessage("用户校验失败,请重新登入!").build();
        //System.out.println((ImageInfo) file);错误
        try {
            if (!faceEngineService.checkIsPortrait(file.getBytes()))
                return ErrorResult.builder().errCode("008").errMessage("图片检验失败,请重新上传!!").build();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //把图片上传到oss
        PicUploadResult upload = picUploadService.upload(file);
        if (upload.getName() == null)
            return ErrorResult.builder().errCode("009").errMessage("图片上传失败,请重新上传!!").build();
        //把头像保存到用户信息表中
        UserInfo userInfo = new UserInfo();
        userInfo.setLogo(upload.getName());
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        if (userInfoMapper.update(userInfo, queryWrapper) == 1) return null;
        return ErrorResult.builder().errCode("010").errMessage("用户注册失败").build();
    }
}
